package com.ym.medical.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ym.core.config.ConfigCenterProperties;
import com.ym.core.exception.BusinessException;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.ComplainRecordDao;
import com.ym.medical.domain.entity.ComplainRecordDetailEntity;
import com.ym.medical.domain.entity.ComplainRecordEntity;
import com.ym.medical.domain.entity.InquiryOrderEntity;
import com.ym.medical.domain.param.ComplainRecordParam;
import com.ym.medical.domain.param.OrderParam;
import com.ym.medical.enums.AuditSpeedEnum;
import com.ym.medical.enums.ComplainStateEnum;
import com.ym.medical.enums.ComplainTypeEnum;
import com.ym.medical.service.IComplainRecordDetailSV;
import com.ym.medical.service.IComplainRecordSV;
import com.ym.medical.service.IInquiryOrderSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * ComputerName:LAPTOP-I6GKDIFQ
 * Author:admin
 * Date: 2019-10-11
 * Time: 10:38:59
 * 服务实现类
 */
@Service
@Transactional( rollbackFor = Exception.class)
public class ComplainRecordSVImpl extends BaseSVImpl<ComplainRecordEntity, ComplainRecordDao> implements IComplainRecordSV {
    @Autowired
    private IComplainRecordDetailSV complainRecordDetailSV;

    @Autowired
    private IInquiryOrderSV inquiryOrderSV;

    @Autowired
    private ConfigCenterProperties configCenterProperties;


    @Override
    public void auditComplainRecord(ComplainRecordParam complainRecord) {
        String auditState = complainRecord.getState();
        Long auditPerson = complainRecord.getAuditor();
        Date auditTime = complainRecord.getAuditTime();
        String auditIdea = complainRecord.getAuditIdea();
        String auditSpeed = complainRecord.getAuditSpeed();   //审核角色

        //1.如果是最后一个人审核或者有人驳回 更改审核工单
        if(ComplainStateEnum.HAS_BEEN_REJECTED.equals(auditState) ||
                AuditSpeedEnum.FINANCIAL.getKey().equals(auditSpeed)){
            mapper.updateByPrimaryKeySelective(complainRecord.covert());
            //订单退费
            if(ComplainStateEnum.HAVE_BEEN_THROUGH.equals(auditState) &&
                    ComplainTypeEnum.ORDERS_FOR_REFUND.getKey().equals(complainRecord.getComplainType())){
                OrderParam orderModel = new OrderParam();
                InquiryOrderEntity order = inquiryOrderSV.selectById(complainRecord.getRelId());
                if(order == null){
                    throw new BusinessException("该订单异常");
                }
                orderModel.setOutTradeNo(order.getIoNum());
                orderModel.setCallbackUrl(configCenterProperties.getMfsConfig().getCallBackUrl());
                String refundUrl = configCenterProperties.getMfsConfig().getRefundUrl();
                String orderModelJson = JSONObject.toJSONString(orderModel);
                //发起退费申请
                RestTemplate restTemplate = new RestTemplate();
                restTemplate.postForObject(refundUrl, orderModelJson, JSONObject.class);
            }
        }

        //2. 保存意见审核记录
        ComplainRecordDetailEntity complainRecordDetail = new ComplainRecordDetailEntity();
        complainRecordDetail.setComplainId(complainRecord.getId());
        complainRecordDetail.setState(auditState);
        complainRecordDetail.setAuditPerson(auditPerson);
        complainRecordDetail.setAuditTime(auditTime);
        complainRecordDetail.setAuditIdea(auditIdea);
        complainRecordDetailSV.insert(complainRecordDetail);
    }
}
